package com.lh.controller;

import com.lh.model.User;
import com.lh.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * 全局异常处理Controller测试类
 *
 * @Author LuoHang
 * @Date 2022-05-09 21:33
 **/
@Controller
@RequestMapping("/exception")
public class ExceptionController {
    public static final Logger logger = LoggerFactory.getLogger(ExceptionController.class);

    @Resource
    private UserService userService;

    /**
     * 测试算数异常请求
     * @param id id
     * @return msg
     */
    @RequestMapping(value = "/arithmeticError")
    public @ResponseBody String arithmeticError(@RequestParam(value = "uid",required = false) Integer id){
        logger.info("开始执行请求");
        User user = userService.getUserById(id);
        int error = 1/0;
        return user.getUsername() + "send a error request";
    }

    /**
     * 测试文件找不到异常请求
     * @param id id
     * @return msg
     * @throws IOException Io异常
     */
    @RequestMapping(value = "/fileNotFindError")
    public @ResponseBody String fileNotFindError(@RequestParam(value = "uid",required = false) Integer id) throws IOException {
        logger.info("开始执行请求");
        User user = userService.getUserById(id);
        FileInputStream in = new FileInputStream("D://error//error.txt");
        in.close();
        return user.getUsername() + "send a error request";
    }

    @GetMapping("/401")
    public String requestBad(){
        return "error/401";
    }

    @GetMapping("/404")
    public String notFound(){
        return "error/404";
    }

    @GetMapping("/500")
    public String serverError(){
        return "error/500";
    }
}
